<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/19
 * Time: 8:49
 */
namespace app\common;
use app\index\model\AuthRuleModel;
use app\index\model\AuthGroupAccessModel;

class Auth{
    protected $uid_rules = array();

    function __construct($uid) {
        $this->uid_rules = $this->getUserRules($uid);
    }

    public function check($route){
        if(in_array($route,$this->uid_rules)){
            return true;
        }else{
            return false;
        }
    }

    //获取用户所有的权限路由
    public function getUserRules($uid){
        $groupAccessModel = new AuthGroupAccessModel();
        $ruleModel = new AuthRuleModel();
        $data = $groupAccessModel->alias('a')
            ->where('uid',$uid)
            ->join('think_auth_group b','b.id=a.group_id')
            ->select();
        $rulesData = array();
        foreach ($data as $k=>$v){
            $infos = $ruleModel->where('id','in',$v['rules'])->field('name')->select();
            foreach ($infos as $k1=>$v1){
                if(!in_array($v1['name'],$rulesData)){
                    array_push($rulesData,$v1['name']);
                }
            }
        }
        return $rulesData;
    }
}